#!/bin/sh

# +++ 变量声明 Start+++
export g_libId="ffmpeg-6.0"
# 源码-库目录
export g_sourceLibDir=${g_inputRootDir}/${g_libId}
# 脚本-库目录
g_scriptLibDir=${g_scriptArchDir}/${g_libId}
# Build-库目录
export g_buildLibDir=${g_buildArchDir}/${g_libId}
if [[ ! -e ${g_buildLibDir} ]]; then
    mkdir -p ${g_buildLibDir}
fi

# Output-库目录
export g_outputLibDir=${g_outputArchDir}/${g_libId}
if [[ ! -e ${g_outputLibDir} ]]; then
    mkdir -p ${g_outputLibDir}
fi
smallSize=$FLAG_YES
export g_flag=""

configCommon() {
    # 输出目录
    g_flag="--prefix=${g_outputLibDir}"
    # 设置pkg,注意--pkg-config的值不要设置错了.这个是默认开启的.
    g_flag="${g_flag} --pkg-config-flags=--static --pkg-config=pkg-config"
    # 各种协议
    g_flag="${g_flag} --enable-gpl --enable-nonfree --enable-version3 --enable-neon"
    g_flag="${g_flag} --enable-pthreads"

    # 库的类型
    g_flag="${g_flag} --disable-shared --enable-static"

    if [[ $smallSize == $FLAG_YES ]]; then
        # 取消所有东西,这个打开,有某些依赖无法满足不能编译出来.
        # g_flag="${g_flag} --disable-everything --disable-autodetect --disable-all"
        # 关闭doc
        g_flag="${g_flag} --disable-doc"
    fi
    # 打开ffplay
    g_flag="${g_flag} --disable-ffplay --disable-sdl2 --disable-ffprobe --disable-ffmpeg"
    g_flag="${g_flag} --enable-libx264"
    g_flag="${g_flag} --enable-videotoolbox"

    # 交叉编译
    # g_flag+=" --target-os=darwin"
    # g_flag+=" --enable-cross-compile"
    # g_flag+=" --arch=$g_arch"

    # g_flag+=" --cc=\"xcrun -sdk iphoneos  clang -target arm64-apple-ios10.0\""

    g_flag+=" --disable-x86asm"
    

}
configDemuxer() {
    if [[ $smallSize == $FLAG_YES ]]; then
        g_flag="${g_flag} --disable-demuxers"
        g_flag="${g_flag}  --enable-demuxer=mov"
        g_flag="${g_flag}  --enable-demuxer=image2"
        g_flag="${g_flag}  --enable-demuxer=image_jpeg_pipe"
        g_flag="${g_flag}  --enable-demuxer=image_png_pipe"
        g_flag="${g_flag}  --enable-demuxer=image_webp_pipe"
        g_flag="${g_flag}  --enable-demuxer=mjpeg"
        g_flag="${g_flag}  --enable-demuxer=aac"
        g_flag="${g_flag}  --enable-demuxer=amr"
        g_flag="${g_flag}  --enable-demuxer=mp3"
        g_flag="${g_flag}  --enable-demuxer=avi"
        g_flag="${g_flag}  --enable-demuxer=mov"
        g_flag="${g_flag}  --enable-demuxer=mpegts"
        g_flag="${g_flag}  --enable-demuxer=mpegvideo"
        g_flag="${g_flag}  --enable-demuxer=rtp"
        g_flag="${g_flag}  --enable-demuxer=rtsp"
        g_flag="${g_flag}  --enable-demuxer=rawvideo"
        g_flag="${g_flag}  --enable-demuxer=wav"
        g_flag="${g_flag}  --enable-demuxer=ogg"
        g_flag="${g_flag}  --enable-demuxer=hls"
        g_flag="${g_flag}  --enable-demuxer=m4v"
        g_flag="${g_flag}  --enable-demuxer=gif"
        g_flag="${g_flag}  --enable-demuxer=matroska"
        g_flag="${g_flag}  --enable-demuxer=rm"
        g_flag="${g_flag}  --enable-demuxer=concat"
        g_flag="${g_flag}  --enable-demuxer=flv"
    fi
}

configParser() {
    if [[ $smallSize == $FLAG_YES ]]; then
        g_flag="${g_flag} --disable-parsers"
        g_flag="${g_flag} --enable-parser=png"
        g_flag="${g_flag} --enable-parser=aac"
        g_flag="${g_flag} --enable-parser=h264"
        g_flag="${g_flag} --enable-parser=mjpeg"
        g_flag="${g_flag} --enable-parser=hevc"
        g_flag="${g_flag} --enable-parser=mpeg4video"
        g_flag="${g_flag} --enable-parser=flac"
        g_flag="${g_flag} --enable-parser=vorbis"
        g_flag="${g_flag} --enable-parser=mpegaudio"
        g_flag="${g_flag} --enable-parser=mpegvideo"
    fi
}

configEncoder() {
    if [[ $smallSize == $FLAG_YES ]]; then
        g_flag="${g_flag} --disable-encoders"
        g_flag="${g_flag} --enable-encoder=rawvideo"
        g_flag="${g_flag} --enable-encoder=pcm_s16le"
        g_flag="${g_flag} --enable-encoder=png"
        g_flag="${g_flag} --enable-encoder=gif"
        g_flag="${g_flag} --enable-encoder=libx264"
        g_flag="${g_flag} --enable-encoder=mpeg4"
        g_flag="${g_flag} --enable-encoder=libhw_avc"
        g_flag="${g_flag} --enable-encoder=h264_videotoolbox"
        g_flag="${g_flag} --enable-encoder=hevc_videotoolbox"
        g_flag="${g_flag} --enable-encoder=mjpeg"
        g_flag="${g_flag} --enable-encoder=aac"
        g_flag="${g_flag} --enable-encoder=mp3"
        g_flag="${g_flag} --enable-encoder=wrapped_avframe"
        g_flag="${g_flag} --enable-encoder=flv"
        g_flag="${g_flag} --enable-encoder=flv"
        g_flag="${g_flag} --enable-encoder=flv"
        g_flag="${g_flag} --enable-encoder=flv"
        g_flag="${g_flag} --enable-encoder=flv"
        g_flag="${g_flag} --enable-encoder=flv"
        g_flag="${g_flag} --enable-encoder=flv"
        g_flag="${g_flag} --enable-encoder=flv"
        g_flag="${g_flag} --enable-encoder=flv"
        g_flag="${g_flag} --enable-encoder=flv"
    fi
}

configMuxer() {
    if [[ $smallSize == $FLAG_YES ]]; then 
    g_flag="${g_flag} --disable-muxers"
    g_flag="${g_flag} --enable-muxer=nut"
    g_flag="${g_flag} --enable-muxer=null"
    g_flag="${g_flag} --enable-muxer=mp4"
    g_flag="${g_flag} --enable-muxer=image2"
    g_flag="${g_flag} --enable-muxer=mp3"
    g_flag="${g_flag} --enable-muxer=flv"
    g_flag="${g_flag} --enable-muxer=mjpeg"
    g_flag="${g_flag} --enable-muxer=gif"
    g_flag="${g_flag} --enable-muxer=singlejpeg"
    g_flag="${g_flag} --enable-muxer=mpegts"
    g_flag="${g_flag} --enable-muxer=rtsp"
    g_flag="${g_flag} --enable-muxer=wav"
    g_flag="${g_flag} --enable-muxer=hls"
    fi
}

configProtocol() {
    if [[ $smallSize == $FLAG_YES ]]; then 
    g_flag="${g_flag} --disable-protocols"
    g_flag="${g_flag} --enable-protocol=file"
    fi
}

configDecoder() {
    if [[ $smallSize == $FLAG_YES ]]; then 
    g_flag="${g_flag}  --disable-decoders"
    g_flag="${g_flag} --enable-decoder=mp3"
    g_flag="${g_flag} --enable-decoder=aac"
    g_flag="${g_flag} --enable-decoder=amrnb"
    g_flag="${g_flag} --enable-decoder=amrwb"
    g_flag="${g_flag} --enable-decoder=png"
    g_flag="${g_flag} --enable-decoder=mjpeg"
    g_flag="${g_flag} --enable-decoder=apng"
    g_flag="${g_flag} --enable-decoder=gif"
    g_flag="${g_flag} --enable-decoder=webp"
    g_flag="${g_flag} --enable-decoder=h264"
    g_flag="${g_flag} --enable-decoder=mpeg4"
    g_flag="${g_flag} --enable-decoder=hevc"
    g_flag="${g_flag} --enable-decoder=h264_mediacodec"
    g_flag="${g_flag} --enable-decoder=hevc_mediacodec"
    g_flag="${g_flag} --enable-decoder=rv10"
    g_flag="${g_flag} --enable-decoder=rv20"
    g_flag="${g_flag} --enable-decoder=rv30"
    g_flag="${g_flag} --enable-decoder=rv40"
    g_flag="${g_flag} --enable-decoder=cook"
    g_flag="${g_flag} --enable-decoder=vorbis"
    g_flag="${g_flag} --enable-decoder=ac3"
    g_flag="${g_flag} --enable-decoder=rawvideo"
    g_flag="${g_flag} --enable-decoder=pcm_alaw"
    g_flag="${g_flag} --enable-decoder=pcm_bluray"
    g_flag="${g_flag} --enable-decoder=pcm_dvd"
    g_flag="${g_flag} --enable-decoder=pcm_f16le"
    g_flag="${g_flag} --enable-decoder=pcm_f24le"
    g_flag="${g_flag} --enable-decoder=pcm_f32le"
    g_flag="${g_flag} --enable-decoder=pcm_f32be"
    g_flag="${g_flag} --enable-decoder=pcm_f64be"
    g_flag="${g_flag} --enable-decoder=pcm_f64le"
    g_flag="${g_flag} --enable-decoder=pcm_lxf"
    g_flag="${g_flag} --enable-decoder=pcm_mulaw"
    g_flag="${g_flag} --enable-decoder=pcm_s16be"
    g_flag="${g_flag} --enable-decoder=pcm_s16be_planar"
    g_flag="${g_flag} --enable-decoder=pcm_s16le"
    g_flag="${g_flag} --enable-decoder=pcm_s16le_planar"
    g_flag="${g_flag} --enable-decoder=pcm_s24be"
    g_flag="${g_flag} --enable-decoder=pcm_s24daud"
    g_flag="${g_flag} --enable-decoder=pcm_s24le"
    g_flag="${g_flag} --enable-decoder=pcm_s24le_planar"
    g_flag="${g_flag} --enable-decoder=pcm_s32be"
    g_flag="${g_flag} --enable-decoder=pcm_s32le"
    g_flag="${g_flag} --enable-decoder=pcm_s32le_planar"
    g_flag="${g_flag} --enable-decoder=pcm_s64be"
    g_flag="${g_flag} --enable-decoder=pcm_s64le"
    g_flag="${g_flag} --enable-decoder=pcm_s8"
    g_flag="${g_flag} --enable-decoder=pcm_s8_planar"
    g_flag="${g_flag} --enable-decoder=pcm_u16be"
    g_flag="${g_flag} --enable-decoder=pcm_u16le"
    g_flag="${g_flag} --enable-decoder=pcm_u24be"
    g_flag="${g_flag} --enable-decoder=pcm_u24le"
    g_flag="${g_flag} --enable-decoder=pcm_u32be"
    g_flag="${g_flag} --enable-decoder=pcm_u32le"
    g_flag="${g_flag} --enable-decoder=pcm_u8"
    g_flag="${g_flag} --enable-decoder=pcm_vidc"
    g_flag="${g_flag} --enable-decoder=pcm_zork"
    g_flag="${g_flag} --enable-decoder=wrapped_avframe"
    fi
}
configIndev() {
    if [[ $smallSize == $FLAG_YES ]]; then 
    g_flag="${g_flag} --disable-indevs"
    fi
}
configFilters() {
    echo ""
    # filter
    # g_flag="${g_flag} --disable-filters --enable-filter=null"
}
configOutdev() {
    echo ""
    # g_flag="${g_flag} --enable-outdev=sdl2"
}
configBsf() {
    echo ""
    # bsf
    # g_flag="${g_flag} --disable-bsfs --enable-bsf=null"
}
configHwaccel() {
    echo ""
    # 加速
    # g_flag="${g_flag} --disable-hwaccels"
}
configDebug() {
    if [[ $g_compileMode == $COMPILE_MODE_DEBUG ]]; then
        g_flag="${g_flag} --disable-stripping --disable-optimizations"
    else
        g_flag="${g_flag} --enable-optimizations --disable-debug"
    fi
}

configCommon
configProtocol
configParser
configDecoder
configDemuxer
configIndev
configOutdev
configFilters
configBsf
configHwaccel
configMuxer
configEncoder
configDebug

function customConfigure {
    ${g_sourceLibDir}/configure ${g_flag} \
    --cc="$g_cc"
}

function customMake {
    make -j4
    make install-headers 
    _AR="xcrun -sdk iphoneos ar"
    find ./ -name "*.o"|xargs $_AR rcs libffmpeg.a
    [[ ! -e $g_outputLibDir/lib ]] && mkdir -p $g_outputLibDir/lib
	cp -f libffmpeg.a ${g_outputLibDir}/lib/
}